<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <link rel="stylesheet" href="css/layui.css">
    <script src="layui.js"></script>
</head>
<body>
    <form class="layui-form">
        <!-- 提示：如果你不想用form，你可以换成div等任何一个普通元素 -->
        <div class="layui-form-item">
            <label class="layui-form-label">用户名</label>
            <div class="layui-input-inline">
                <input type="text" name="un" id="un" placeholder="请输入" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">密码</label>
            <div class="layui-input-inline">
                <input type="text" name="pwd" id="pwd" placeholder="请输入" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-input-block">
                <button class="layui-btn" lay-submit lay-filter="*">立即提交</button>
                <button type="reset" class="layui-btn layui-btn-primary">重置</button>
            </div>
        </div>
        <!-- 更多表单结构排版请移步文档左侧【页面元素-表单】一项阅览 -->
    </form>
    <script>
        layui.use(['form','layer'], function () {
            var form = layui.form;
            var $ = layui.$;
            var data = layui.data;
            var layer=layui.layer;

            //console.log(data);
            form.on('submit(*)', function (data) {
                console.log(data.elem) //被执行事件的元素DOM对象，一般为button对象
                console.log(data.form) //被执行提交的form对象，一般在存在form标签时才会返回
                console.log(data.field) //当前容器的全部表单字段，名值对形式：{name: value}
                $.post(
                    'http://stuapi.ysdjypt.com/api/login'
                    , {
                         un: $("#un").val()
                        , pwd: $("#pwd").val()
                        , token: '69f84759-5242-488f-b98c-88b8e504dd95'
                    }
                    , function (res) {
                        if(res.success){
                            layer.msg("用户登录成功");
                            location.href='http://127.0.0.1:5500/start/index.html';
                        }else{
                            layer.msg("用户登录失败");
                        }
                        //console.log(res.success);
                    }, 'json'
                );
                return false; //阻止表单跳转。如果需要表单跳转，去掉这段即可。
            });

            //各种基于事件的操作，下面会有进一步介绍
        });
    </script>
</body>
</html>